package org.example.panel.studentpanel;
import javax.swing.*;
import java.awt.event.*;
import org.example.operation.*;

/**
 * @author lenovo
 */

public class ModifyStudentPanel extends JPanel {
    public ModifyStudentPanel(){
        //组件准备
        JTextField num = new JTextField(15);
        JTextField name = new JTextField(15);
        JTextField gender = new JTextField(15);
        JTextField birthday = new JTextField(15);
        JButton addStudent = new JButton("修改学生");
        Box numBox = Box.createHorizontalBox();
        Box nameBox = Box.createHorizontalBox();
        Box genderBox = Box.createHorizontalBox();
        Box birthdayBox = Box.createHorizontalBox();
        Box box = Box.createVerticalBox();
        //注册监视器
        addStudent.addActionListener((ActionEvent e)->{
            if(num.getText().isEmpty()) {
                JOptionPane.showMessageDialog(null,
                        "学号不能为空", null, JOptionPane.WARNING_MESSAGE);
            }else if(num.getText().matches(".*\\s+.*") ||
                    name.getText().matches(".*\\s+.*") ||
                    gender.getText().matches(".*\\s+.*") ||
                    birthday.getText().matches(".*\\s+.*")){
                JOptionPane.showMessageDialog(null,
                        "信息不能有空格", null, JOptionPane.WARNING_MESSAGE);
            }else if(StudentOperation.isNotStudent(num.getText())){
                JOptionPane.showMessageDialog(null,
                        "学生不存在", null, JOptionPane.WARNING_MESSAGE);
            }else{
                StudentOperation.modifyStudent(num.getText(), name.getText(),
                        gender.getText(), birthday.getText());
                JOptionPane.showMessageDialog(null,
                        "修改学生成功", null, JOptionPane.WARNING_MESSAGE);
            }
        });
        //添加组件
        numBox.add(new JLabel("学号： "));
        numBox.add(num);
        nameBox.add(new JLabel("姓名： "));
        nameBox.add(name);
        genderBox.add(new JLabel("性别： "));
        genderBox.add(gender);
        birthdayBox.add(new JLabel("出生日期： "));
        birthdayBox.add(birthday);
        box.add(numBox);
        box.add(Box.createVerticalStrut(5));
        box.add(nameBox);
        box.add(Box.createVerticalStrut(5));
        box.add(genderBox);
        box.add(Box.createVerticalStrut(5));
        box.add(birthdayBox);
        box.add(Box.createVerticalStrut(5));
        box.add(addStudent);
        add(box);
    }
}
